package com.tomtom.amelinium.chartservice;

import java.util.ArrayList;

import com.tomtom.amelinium.backlogservice.BacklogServiceFactory;
import com.tomtom.amelinium.backlogservice.builders.BacklogModelBuilder;
import com.tomtom.amelinium.backlogservice.corrector.BacklogModelCorrector;
import com.tomtom.amelinium.backlogservice.model.BacklogModel;
import com.tomtom.amelinium.chartservice.builders.ChartModelBuilder;
import com.tomtom.amelinium.chartservice.corrector.TableCorrector;
import com.tomtom.amelinium.chartservice.model.ChartModel;
import com.tomtom.amelinium.chartservice.serializer.ChartSerializer;
import com.tomtom.amelinium.chartservice.serializer.TabsHTMLChartSerializer;
import com.tomtom.amelinium.common.LineProducer;

public class ChartServiceFactory {

	public ChartModel readChartModel(String chartFileName) {

		LineProducer producer = new LineProducer();
		ChartModelBuilder chartModelBuilder = new ChartModelBuilder();

		ArrayList<String> chartLines = producer
				.readLinesFromFile(chartFileName);

		ChartModel chartModel = chartModelBuilder.buildChartModel(chartLines);

		return chartModel;

	}

	public ChartModel readAndCorrectChartModel(String backlogFileName,
			String chartFileName) {

		BacklogServiceFactory backlogServiceFactory = new BacklogServiceFactory();
		TableCorrector tableCorrector = new TableCorrector();

		BacklogModel backlogModel = backlogServiceFactory
				.readAndCorrectBacklogModel(backlogFileName);
		ChartModel chartModel = readChartModel(chartFileName);

		tableCorrector.correctTables(backlogModel, chartModel);

		return chartModel;

	}

	public String readCorrectAndSerializeChartModelFromFiles(
			String backlogFileName, String chartFileName) {

		BacklogServiceFactory backlogServiceFactory = new BacklogServiceFactory();
		ChartSerializer chartSerializer = new ChartSerializer();

		BacklogModel backlogModel = backlogServiceFactory
				.readAndCorrectBacklogModel(backlogFileName);
		ChartModel chartModel = readAndCorrectChartModel(backlogFileName,
				chartFileName);

		String output = chartSerializer.serializeChartModel(chartModel,
				backlogModel);

		return output;

	}

	public String readCorrectAndSerializeChartModelFromStrings(
			String oldBacklog, String oldChart) {
		LineProducer producer = new LineProducer();
		ChartModelBuilder chartModelBuilder = new ChartModelBuilder();
		BacklogModelBuilder backlogModelBuilder = new BacklogModelBuilder();
		ChartSerializer chartSerializer = new ChartSerializer();
		TableCorrector tableCorrector = new TableCorrector();
		BacklogModelCorrector backlogModelCorrector = new BacklogModelCorrector();

		ArrayList<String> chartLines = producer.readLinesFromString(oldChart);
		ChartModel chartModel = chartModelBuilder.buildChartModel(chartLines);

		ArrayList<String> backlogLines = producer
				.readLinesFromString(oldBacklog);
		BacklogModel backlogModel = backlogModelBuilder
				.buildBacklogModel(backlogLines);

		backlogModelCorrector.correctModelPoints(backlogModel);
		tableCorrector.correctTables(backlogModel, chartModel);

		String newChartContent = chartSerializer.serializeChartModel(
				chartModel, backlogModel);

		return newChartContent;
	}

	public String serializeChartModelFromFiles(String backlogFileName,
			String chartFileName) {
		BacklogServiceFactory backlogServiceFactory = new BacklogServiceFactory();
		ChartSerializer chartSerializer = new ChartSerializer();

		BacklogModel backlogModel = backlogServiceFactory
				.readBacklogModel(backlogFileName);
		ChartModel chartModel = readChartModel(chartFileName);

		String output = chartSerializer.serializeChartModel(chartModel,
				backlogModel);

		return output;
	}

	public String serializeWikiMarkupFromFileToJSPString(String fileName) {
		LineProducer producer = new LineProducer();
		ChartModelBuilder chartModelBuilder = new ChartModelBuilder();
		TabsHTMLChartSerializer htmlChartSerializer = new TabsHTMLChartSerializer();
		ArrayList<String> chartLines = producer.readLinesFromFile(fileName);
		ChartModel chartModel = chartModelBuilder.buildChartModel(chartLines);

		String content = htmlChartSerializer.generateHTML(chartModel);

		return content;

	}

	public String correctAndSerializeWikiMarkupFromFilesToJSPString(
			String backlogFileName, String chartFileName) {

		TabsHTMLChartSerializer htmlChartSerializer = new TabsHTMLChartSerializer();

		ChartModel chartModel = readAndCorrectChartModel(backlogFileName,
				chartFileName);

		String output = htmlChartSerializer.generateHTML(chartModel);

		return output;

	}

}
